import { request } from "../../utils/http"
import { getAnswerResult, isEmpty, questionTypeSelect } from "../../utils/util"

Page({
  data: {
    questionTypeSelect,
    pageData: {},
    examId: null,
  
    currentIndex: 0,
    tid: null,

    knowledge: '',
    answers: [],
    question: {},
    userAnswers: [],
    answerResult: null,
    showAnswer: false,
    isEnded: true
  },

  selectChange (e) {
    const { value } = e.detail
    const isQa = this.data.question.type === questionTypeSelect.QA
    const selectIds = isQa ? this.data.answers.map(_ => _.id) : value
    this.setData({
      userAnswers: selectIds.map(id => {
        const item = this.data.answers.find(_ => _.id === +id)
        return {
          achievementQuestionId: this.data.question.achievementQuestion.id,
          examId: this.data.examId,
          questionId: this.data.question.id,
          ...(
            isQa ? { answerContent: value } : { answerChoice: item.choice }
          )
        }
      })
    })
  },

  setQuestionData (data) {
    if (!data) {
      data = this.data.pageData
    }
    this.setData({
      isEnded: false,
      answerResult: null,
      showAnswer: false,
      userAnswers: [],
      answers: data.answers || [],
      knowledge: (data.knowledges || [])[0],
      question: { ...data, typeName: !isEmpty(data.type) && questionTypeSelect.getLabel(data.type) }
    })
  },

  postAnswer () {
    const { userAnswers } = this.data
    const noAnswer = userAnswers.length < 1
    if (this.data.question.type === questionTypeSelect.QA) {
      if (noAnswer || userAnswers.some(_ => !(_.answerContent || '').trim())) {
        return wx.showToast({ title: '请填写答案！' })
      }
    } else if (noAnswer) {
      return wx.showToast({ title: '请选择答案！' })
    }

    const { achievementQuestion: q = {} } = this.data.question
    wx.showLoading({ title: '提交中...' })
    request({
      method: 'POST',
      url: `/api/portal/exam/achievementAnswer/${q.id}|submit`,
      data: userAnswers
    }).then(async data => {
      await wx.hideLoading()
      this.setData({
        answerResult: data
      })
      const result = getAnswerResult(data.correctness)
      wx.showToast({ title: result.label, image: result.icon })
    }).catch(e => {
      wx.hideLoading()
    })
  },

  viewAnswer () {
    this.setData({
      showAnswer: true
    })
  },

  getQuestion () {
    wx.showLoading()
    request({
      method: 'POST',
      url: `/api/portal/exam/achievementQuestion/0|start`
    }).then(data => {
      wx.hideLoading()
      this.setData({
        pageData: data
      })
      this.setQuestionData(data)
      const isQa = this.data.question.type === questionTypeSelect.QA
      if (isQa) {
        const answerSelect = this.selectComponent('#answerSelect')
        answerSelect && answerSelect.clearInput && answerSelect.clearInput()
      }
    }).catch(async e => {
      await wx.hideLoading()
      wx.showToast({ title: e.message })
    })
  },

  goNextQuestion () {
    const nextIndex = this.data.currentIndex + 1
    this.setData({
      currentIndex: nextIndex
    })
    this.getQuestion()
  },

  endPractise () {
    this.setData({
      isEnded: true
    })
  }
})
